//add map area panelId 
var dopBtnstrategyAdd = "";
var mapManager = null;
var isModify = false;
$("#dop_datagrid_strategy").datagrid({
	url : "",
	height : 350,
	pagination : true,
	fitColumns : false,
	singleSelect : false,
	fit : true,
	pageSize : 10,
	pageList : [10, 30, 50, 100],
	sortOrder : 'desc',
	rownumbers : true,
	showFooter : true,
	frozenColumns : [[]],
	columns : [[{
		field : 'ck',
		checkbox : true
	}, {
		field : "id",
		width : 150,
		title : "主键",
		hidden : true
	}, {
		field : "name",
		width : 150,
		title : "名称"
	}, {
		field : "isEnableText",
		width : 150,
		title : "启用"
	}]]
});

init();

// click pagination
var p = $("#dop_datagrid_strategy").datagrid('getPager');
$(p).pagination(
		{
			onSelectPage : function(pageNumber, pageSize) {
				var params = $.extend({
					pageNum : pageNumber,
					pageSize : pageSize
				}, getQueryParams());
				EapTools.doAction("distributeStrategyController.do?queryList",
						params, true, function(data) {
							$("#dop_datagrid_strategy").datagrid('loadData', {
								"rows" : data.list,
								"total" : data.count
							} || {});
						});
			}
});


function comparableSlider(sliderId, inputId) {
	$(sliderId).slider({
		value : 50,
		min : 0,
		max : 100,
		step : 1,
		slide : function(event, ui) {
			$(inputId).val(ui.value);
		}
	});
	$(inputId).val($(sliderId).slider("value"));
}
function matchableSlider(sliderId, inputId) {
	$(sliderId).slider({
		value : 50,
		min : 0,
		max : 100,
		step : 100,
		slide : function(event, ui) {
			$(inputId).val(ui.value);
		}
	});
	$(inputId).val($(sliderId).slider("value"));
}

$(function() {
	$("#sortable").sortable({
		revert : true
	});
	$("ul, li").disableSelection();
});

function getOrder() {
	var dataArray = [];
	$("#sortable input").each(function() {
		var $this = $(this);
		dataArray.push("{", $this.attr("id"), ":", $this.val(), "}");
	});

	$("#showOrder").html(dataArray.join("<br>"));
}


// bind add event
$("#dop_btn_strategy_add")
		.click(
				function() {
					isModify = false;
					$("#sortable").empty();
					
					EapTools.doAction("distributeStrategyController.do?getPresetCondition",
							{}, false, function(data) {
								for(var i=0;i<data.length;i++){
									var id=data[i].id;
									var name=data[i].name
									var sliderId=data[i].id+"Slider";
									$("#sortable")
									.append(
											'<li><p> <label for="'+id+'" style="font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif;">'+name+'：</label> <input type="text" id="'+id+'" readonly="true" style="border: 0; color: #f6931f; font-weight: bold;"></p><div id="'+sliderId+'"></div></li>');
									comparableSlider("#"+sliderId, "#"+id);
								}
							});
					
//					$("#sortable")
//							.append(
//									'<li><p> <label for="deliveryArea" style="font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif;">配送区域：</label> <input type="text" id="deliveryArea" readonly="true" style="border: 0; color: #f6931f; font-weight: bold;"></p><div id="areaSlider"></div></li>');
//					$("#sortable").append(
//							'<li><p> <label for="deliveryDistance" style="font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif;">配送区域：</label> <input type="text" id="deliveryDistance" readonly="true" style="border: 0; color: #f6931f; font-weight: bold;"></p><div id="distanceSlider"></div></li>');
//					comparableSlider("#distanceSlider", "#deliveryDistance");
//					matchableSlider("#areaSlider", "#deliveryArea");
//					func("#distanceSlider", "#deliveryDistance");
//					func("#belongTeamSlider", "#belongTeam");
//					func("#busyLevelSlider", "#busyLevel");
					
					dopBtnstrategyAdd = jQuery
							.layer({
								type : 1,
								title : '<div><span style="padding-left: 14px;line-height: 14px;">添加分单策略</span></div>',
								shadeClose : true,
								maxmin : false,
								fix : false,
								area : [550, 500],
								page : {
									dom : '#dop_strategy_add_panel'
								}
							});
				});

// bind modify event
$("#dop_btn_strategy_modify").click(function() {
	var selections = $("#dop_datagrid_strategy").datagrid("getSelections");
	if (selections && selections.length > 0) {
		if (selections.length > 1) {
			layer.tips('请选择一行数据', this, {
				guide : 2,
				time : 2
			});
		} else {
			// 显示地图div
			//$("#dop_btn_strategy_add").click();
			EapTools.doAction("distributeStrategyController.do?getPresetCondition",
					{}, false, function(data) {
						for(var i=0;i<data.length;i++){
							var id=data[i].id;
							var name=data[i].name
							var sliderId=data[i].id+"Slider";
							$("#sortable")
							.append(
									'<li><p> <label for="'+id+'" style="font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif;">'+name+'：</label> <input type="text" id="'+id+'" readonly="true" style="border: 0; color: #f6931f; font-weight: bold;"></p><div id="'+sliderId+'"></div></li>');
							comparableSlider("#"+sliderId, "#"+id);
						}
					});
			
			dopBtnstrategyAdd = jQuery
					.layer({
						type : 1,
						title : '<div><span style="padding-left: 14px;line-height: 14px;">修改分单策略</span></div>',
						shadeClose : true,
						maxmin : false,
						fix : false,
						area : [550, 500],
						page : {
							dom : '#dop_strategy_add_panel'
						}
					});
			// 获得此项记录的坐标字符串
			var resName = selections[0].resName;
			// 更新区域名称的显示
			$("input[id='name']").val(resName);
			$("#name").val(selections[0].name);
			isModify = true;
		}
	} else
		layer.tips('请选择要修改行', this, {
			guide : 2,
			time : 2
		});
});

// bind delete event
$("#dop_btn_strategy_delete").click(function() {
	var selections = $("#dop_datagrid_strategy").datagrid("getSelections");
	if (selections && selections.length > 0)
		layer.confirm('确定删除吗？', function() {
			var arr=[];
			for (var i=0; i < selections.length; i++) {
				arr.push(selections[i].id);
			}
			EapTools.doAction("distributeStrategyController.do?delete", {
				"idsList" : arr
			}, false, function(data) {
				if (data.success) {
					EapTip.msgOk("删除成功！");
					init();
				}
			});
		});
	else
		layer.tips('请选择要删除行', this, {
			guide : 2,
			time : 2
		});
});

//停用按钮事件
$("#dop_btn_strategy_stop").click(function(){
	var selected=$("#dop_datagrid_strategy").datagrid("getSelections");
	if(selected.length>0){
		layer.confirm("确定要停用吗？",function(){
			var arr=[];
			for (var i=0; i < selected.length; i++) {
				arr.push(selected[i].id);
			}
			EapTools.doAction(
					"distributeStrategyController.do?stop", {
						"idsList" : arr
					}, true, function(data) {
						if (data.success) {
							EapTip.msgOk("操作成功！");
							init();
							selected.length=0;
						}
					});
		});
	}else{
		layer.tips('请选择要停用的行！', this, {
			guide : 2,
			time : 2
		});
	}
});
//启用按钮事件
$("#dop_btn_strategy_open").click(function(){
	var selected=$("#dop_datagrid_strategy").datagrid("getSelections");
	if(selected.length==1){
		layer.confirm("确定要启用吗？",function(){
			var arr=[];
			for (var i=0; i < selected.length; i++) {
				arr.push(selected[i].id);
			}
			EapTools.doAction(
					"distributeStrategyController.do?open", {
						"id" : selected[0].id
					}, true, function(data) {
						if (data.success) {
							EapTip.msgOk("操作成功！");
							init();
							selected.length=0;
						}
					});
		});
		
	}else if(selected.length>1){
		layer.tips('只能启用一条！', this, {
			guide : 2,
			time : 2
		});
	}else{
		layer.tips('请选择一条数据进行操作！', this, {
			guide : 2,
			time : 2
		});
	}
});

// 添加面板 确定按钮
$("#dop_strategy_add_panel_ok").click(
		function() {
			var name = $("input[id='name']").val();
			if (name) {
				var conditionArray=new Array();
				$("#sortable input").each(
						function() {
							var $this = $(this);
							conditionArray.push($this.attr("id")+":"+$this.val());
						});
				if (isModify) {
					EapTools.doAction("distributeStrategyController.do?modify",
							{
								"name" : name,
								"id" : $("#dop_datagrid_strategy").datagrid(
										"getSelections")[0].id,
								"conditionStrList" : conditionArray
							}, false, function(data) {
								layer.close(dopBtnstrategyAdd);
								EapTip.msgOk("修改成功");
								$("input[id='resName']").val("");
								init();
							});
				} else {
					EapTools.doAction("distributeStrategyController.do?create",
							{
								"name" : name,
								"conditionStrList" : conditionArray
							}, false, function(data) {
								layer.close(dopBtnstrategyAdd);
								EapTip.msgOk("添加成功");
								$("input[id='name']").val("");
								init();
							});
				}
			} else {
				layer.tips('请填写策略名称', document.getElementById("name"), {
					guide : 2,
					time : 2
				});
			}
		});

// 添加面板 取消按钮
$("#dop_strategy_add_panel_cancel").click(function() {
	layer.close(dopBtnstrategyAdd);
	$("#name").val("");
});

// bind query event
$("#dop_btn_strategy_query").click(function() {
	init();
});

// get params
function getQueryParams() {
	return {
		name : $("input[code='name']").val()
	};
}

// init
function init() {
	// init
	var options = $("#dop_datagrid_strategy").datagrid('getPager').data(
			"pagination").options;
	var pageSize = options.pageSize;
	var params = $.extend({
		pageNum : 1,
		pageSize : pageSize
	}, getQueryParams());
	EapTools.doAction("distributeStrategyController.do?queryList", params,
			false, function(data) {
				$("#dop_datagrid_strategy").datagrid('loadData', {
					"rows" : data.list,
					"total" : data.count
				} || {});
				var p = $("#dop_datagrid_strategy").datagrid('getPager');
				$("td:last", p).find("a").click();
			});
}

